﻿using AngleExpress.utils;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace AngleExpress.software
{
    //申通梧桐
    class ShenTongExpress : ISoftware, IDisposable
    {
        private bool disposedValue;
        private readonly string driverProcName = "electronchromedriver";
        private readonly string wuTongProcName = "STO.WuTong";
        private readonly string driverExePath;
        private readonly string nodeExePath;
        private readonly string scriptPath;
        private readonly string wuTongExePath;

        internal ShenTongExpress()
        {
            driverExePath = System.AppDomain.CurrentDomain.BaseDirectory + "electron-chrome-driver\\" + driverProcName + ".exe";
            nodeExePath = System.AppDomain.CurrentDomain.BaseDirectory + "node.exe";
            scriptPath = System.AppDomain.CurrentDomain.BaseDirectory + "stscript\\main.js";
            wuTongExePath = System.AppDomain.CurrentDomain.BaseDirectory + "st\\App\\" + wuTongProcName + ".exe";
        }

        public override void OpenSoftware(string username, string password, string otherParam)
        {
            TextLog.Add(driverExePath);
            TextLog.Add(nodeExePath);
            TextLog.Add(scriptPath);
            TextLog.Add(wuTongExePath);
            if (!File.Exists(driverExePath) || !File.Exists(nodeExePath) || !File.Exists(scriptPath) || !File.Exists(wuTongExePath))
            {
                throw new Exception("找不到所需可执行文件!");
            }

            //if (IsProcExist(wuTongProcName))
            //{
            //    MessageBox.Show("该程序已启动", "异常", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
            //    return;
            //}

            if (!IsProcExist(driverProcName))
            {
                System.Diagnostics.Process driverProc = new System.Diagnostics.Process();
                driverProc.StartInfo.FileName = driverExePath;
                driverProc.StartInfo.CreateNoWindow = true;
                driverProc.StartInfo.UseShellExecute = false;
                driverProc.Start();
            }

            System.Diagnostics.Process nodeScript = new System.Diagnostics.Process();
            nodeScript.StartInfo.FileName = nodeExePath;
            if (otherParam != null)
                nodeScript.StartInfo.Arguments = "\"" + scriptPath + "\" \"" + username + "\" \"" + password + "\" \"" + otherParam + "\"";
            else
                nodeScript.StartInfo.Arguments = "\"" + scriptPath + "\" \"" + username + "\" \"" + password + "\"";
            nodeScript.StartInfo.CreateNoWindow = true;
            nodeScript.StartInfo.UseShellExecute = false;
            nodeScript.Start();

        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                }
                KillProc(driverProcName);
                disposedValue = true;
            }
        }

        ~ShenTongExpress()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
}
